#include "game.h"
#include "ui_game.h"

game::game(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::game),blsRun(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(100,100,1200,800));
}

game::~game()
{
    delete ui;
}

void game::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if(!blsRun)
    {
        InitCat();
    }

    painter.setPen(Qt::blue);
    painter.setBrush(Qt::gray);
    painter.drawRect(10,10,980,780);

    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawRect(rock_1);
    painter.drawRect(rock_2);
    painter.drawRect(rock_3);

    if(is_bee_1)
    {
        painter.setPen(Qt::yellow);
        painter.setBrush(Qt::yellow);
        painter.drawRect(Bee_1);
    }

    if(is_bee_2)
    {
        painter.setPen(Qt::yellow);
        painter.setBrush(Qt::yellow);
        painter.drawRect(Bee_2);
    }

    if(run_time>=30&&run_time%10==0)
    {
        painter.setPen(Qt::lightGray);
        painter.setBrush(Qt::lightGray);
        painter.drawRect(recovery);
    }

    painter.setPen(Qt::cyan);
    painter.setBrush(Qt::cyan);
    painter.drawRect(vCatRect);

    QFont font_1("Courier",100);
    painter.setFont(font_1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(450,400,sDisplay);

    QFont font_2("Courier",100);
    painter.setFont(font_2);
    painter.setPen(Qt::magenta);
    painter.setBrush(Qt::magenta);
    painter.drawText(1000,200,"历程：");
    painter.drawText(1060,200,QString::number(run_time));
    painter.drawText(1180,200,"m");

    QFont font_3("Courier",100);
    painter.setFont(font_3);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(1000,250,"精力值：");
    painter.drawText(1100,250,QString::number(Hp));
    painter.drawText(1000,300,"疼痛值:");
    painter.drawText(1150,300,QString::number(bee_hit_times));

    if(Hp<=0)
    {
        blsOver=true;
    }

    if(blsOver)
    {
        timer->stop();
        timer_1->stop();
        timer_2->stop();
    }

    if(!blsOver)
    {
        if(blsPause)
        {
            timer->stop();
            timer_1->stop();
            timer_2->stop();

        }
        else
        {
            timer->start();
        }
    }
}

void game::InitCat()
{
    nDirection=0;
    Hp=3;
    blsRun=true;
    blsOver=false;
    blsPause=false;
    is_bee_1=false;
    is_bee_2=false;
    rock_1=CreateRock_1();
    rock_2=CreateRock_2();
    rock_3=CreateRock_3();
    Bee_1=CreateBee_1();
    Bee_2=CreateBee_2();
    recovery=CreateRecovery();
    QRect rect(100,480,60,40);
    vCatRect=rect;
    timer=new QTimer(this);
    timer_1=new QTimer(this);
    timer_2=new QTimer(this);
    timer->start(10);
    timer_1->start(2000);
    timer_2->start(15);
    connect(timer,SIGNAL(timeout()),SLOT(Cat_update()));
    connect(timer,SIGNAL(timeout()),SLOT(rock_1_update()));
    connect(timer,SIGNAL(timeout()),SLOT(rock_2_update()));
    connect(timer,SIGNAL(timeout()),SLOT(rock_3_update()));
    connect(timer_1,SIGNAL(timeout()),SLOT(run_time_update()));
    connect(timer,SIGNAL(timeout()),SLOT(bee_1_on()));
    connect(timer,SIGNAL(timeout()),SLOT(bee_1_update()));
    connect(timer,SIGNAL(timeout()),SLOT(bee_2_on()));
    connect(timer,SIGNAL(timeout()),SLOT(bee_2_update()));
    connect(timer_2,SIGNAL(timeout()),SLOT(rock_move_step_update()));
    connect(timer_1,SIGNAL(timeout()),SLOT(recovery_update()));
}



void game::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key())
    {
    case Qt::Key_W:
        nDirection=1;
        break;
    case Qt::Key_D:
        nDirection=2;
        break;
    case Qt::Key_S:
        nDirection=3;
        break;
    case Qt::Key_A:
        nDirection=4;
        break;
    case Qt::Key_Shift:
        Cat_step=2;
        break;
    case Qt::Key_K:
        nDirection=0;
        break;
    default:;
    }
}

void game::keyReleaseEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent *)event;
    switch(key->key())
    {
    case Qt::Key_Shift:Cat_step=1;
        break;
    case Qt::Key_W:
        nDirection=0;
        break;
    case Qt::Key_D:
        nDirection=0;
        break;
    case Qt::Key_S:
        nDirection=0;
        break;
    case Qt::Key_A:
        nDirection=0;
        break;
    default:;
    }
}

void game::Cat_update()
{
    IsHit_rock_1();
    IsHit_rock_2();
    IsHit_rock_3();
    IsHit_bee_1();
    IsHit_bee_2();
    IsHit_recovery();
    switch(nDirection)
    {
    case 0:
        break;
    case 1:
        if((vCatRect.top()>=11&&Cat_step==1)||(vCatRect.top()>=12&&Cat_step==2))
        {
            vCatRect.setTop(vCatRect.top()-Cat_step);
            vCatRect.setBottom(vCatRect.bottom()-Cat_step);
        }
        break;
    case 2:
        if((vCatRect.right()<=989&&Cat_step==1)||(vCatRect.right()<=988&&Cat_step==2))
        {
            vCatRect.setRight(vCatRect.right()+Cat_step);
            vCatRect.setLeft(vCatRect.left()+Cat_step);
        }
        break;
    case 3:
        if((vCatRect.bottom()<=789&&Cat_step==1)||(vCatRect.bottom()<=788&&Cat_step==2))
        {
         vCatRect.setTop(vCatRect.top()+Cat_step);
         vCatRect.setBottom(vCatRect.bottom()+Cat_step);
        }
        break;
    case 4:
        if((vCatRect.left()>=11&&Cat_step==1)||(vCatRect.left()>=12&&Cat_step==2))
        {
            vCatRect.setRight(vCatRect.right()-Cat_step);
            vCatRect.setLeft(vCatRect.left()-Cat_step);
        }
        break;
    default:;
    }
    update();
}

void game::rock_1_update()
{
    if(rock_1.left()<=10)
    {
        int x,y;
        x=qrand()%30;
        y=qrand()%730;
        QRect rect(940+x,10+y,40,40);
        rock_1=rect;
    }
    else
    {
        rock_1.setLeft(rock_1.left()-rock_move_step);
        rock_1.setRight(rock_1.right()-rock_move_step);
    }
    update();
}

void game::rock_2_update()
{
    if(rock_2.left()<=10)
    {
        int x,y;
        x=qrand()%30;
        y=qrand()%730;
        QRect rect(940+x,10+y,40,40);
        rock_2=rect;
    }
    else
    {
        rock_2.setLeft(rock_2.left()-rock_move_step);
        rock_2.setRight(rock_2.right()-rock_move_step);
    }
    update();
}

void game::rock_3_update()
{
    if(rock_3.left()<=10)
    {
        int x,y;
        x=qrand()%30;
        y=qrand()%730;
        QRect rect(940+x,10+y,40,40);
        rock_3=rect;
    }
    else
    {
        rock_3.setLeft(rock_3.left()-rock_move_step);
        rock_3.setRight(rock_3.right()-rock_move_step);
    }
    update();
}

QRect game::CreateRock_1()
{
    int x,y;
    x=qrand()%30;
    y=qrand()%740;
    QRect rect(930+x,10+y,40,40);
    return rect;
}

QRect game::CreateRock_2()
{
    int x,y;
    x=qrand()%30;
    y=qrand()%740;
    QRect rect(930+x,10+y,40,40);
    return rect;
}

QRect game::CreateRock_3()
{
    int x,y;
    x=qrand()%30;
    y=qrand()%740;
    QRect rect(930+x,10+y,40,40);
    return rect;
}

void game::IsHit_rock_1()
{
    if(vCatRect.top()>=rock_1.top()&&vCatRect.top()<=rock_1.bottom()&&vCatRect.right()>=rock_1.left()&&vCatRect.right()<=rock_1.right())
    {
        sDisplay="喵喵被石头绊倒了！";
        Hp=0;
        update();
    }
    if(vCatRect.bottom()>=rock_1.top()&&vCatRect.bottom()<=rock_1.bottom()&&vCatRect.right()>=rock_1.left()&&vCatRect.right()<=rock_1.right())
    {
        sDisplay="喵喵被石头绊倒了！";
        Hp=0;
        update();
    }
}

void game::IsHit_rock_2()
{
    if(vCatRect.top()>=rock_2.top()&&vCatRect.top()<=rock_2.bottom()&&vCatRect.right()>=rock_2.left()&&vCatRect.right()<=rock_2.right())
    {
        sDisplay="喵喵被石头绊倒了！";
        Hp=0;
        update();
    }
    if(vCatRect.bottom()>=rock_2.top()&&vCatRect.bottom()<=rock_2.bottom()&&vCatRect.right()>=rock_2.left()&&vCatRect.right()<=rock_2.right())
    {
        sDisplay="喵喵被石头绊倒了！";
        Hp=0;
        update();
    }
}

void game::IsHit_rock_3()
{
    if(vCatRect.top()>=rock_3.top()&&vCatRect.top()<=rock_3.bottom()&&vCatRect.right()>=rock_3.left()&&vCatRect.right()<=rock_3.right())
    {
        sDisplay="喵喵被石头绊倒了！";
        Hp=0;
        update();
    }
    if(vCatRect.bottom()>=rock_3.top()&&vCatRect.bottom()<=rock_3.bottom()&&vCatRect.right()>=rock_3.left()&&vCatRect.right()<=rock_3.right())
    {
        sDisplay="喵喵被石头绊倒了！";
        Hp=0;
        update();
    }
}

void game::rock_move_step_update()
{
    if(run_time>=0&&run_time<50)
    {
        rock_move_step=1;
    }
    if(run_time>=50&&run_time<150)
    {
        rock_move_step=2;
    }
    if(run_time>=150)
    {
        rock_move_step=3;
    }
    update();
}

QRect game::CreateBee_1()
{
    int x,y;
    x=qrand()%30;
    y=qrand()%640;
    QRect rect_bee(930+x,110+y,40,40);
    return rect_bee;
}

QRect game::CreateBee_2()
{
    int x,y;
    x=qrand()%30;
    y=qrand()%640;
    QRect rect_bee(930+x,110+y,40,40);
    return rect_bee;
}

void game::bee_1_on()
{
    if(!is_bee_1&&run_time!=0&&run_time%20==0)
    {
        is_bee_1=true;
    }
    update();
}

void game::bee_2_on()
{
    if(!is_bee_2&&run_time!=0&&run_time%100==0)
    {
        is_bee_2=true;
    }
    update();
}

void game::bee_1_update()
{
    if(is_bee_1)
    {
        Bee_1.setLeft(Bee_1.left()-2);
        Bee_1.setRight(Bee_1.right()-2);
        if(bee_1_move_step%100>=0&&bee_1_move_step%100<=50)
        {
            Bee_1.setTop(Bee_1.top()-2);
            Bee_1.setBottom(Bee_1.bottom()-2);
        }
        else
        {
            Bee_1.setTop(Bee_1.top()+2);
            Bee_1.setBottom(Bee_1.bottom()+2);
        }
        bee_1_move_step++;
        bee_1_move_step%=100;
        if(Bee_1.left()<=10)
        {
            int x,y;
            x=qrand()%30;
            y=qrand()%640;
            QRect rect_bee(930+x,110+y,40,40);
            Bee_1=rect_bee;
        }
    }
    update();
}

void game::bee_2_update()
{
    if(is_bee_2)
    {
        Bee_2.setLeft(Bee_2.left()-2);
        Bee_2.setRight(Bee_2.right()-2);
        if(bee_2_move_step%100>=0&&bee_2_move_step%100<=50)
        {
            Bee_2.setTop(Bee_2.top()-2);
            Bee_2.setBottom(Bee_2.bottom()-2);
        }
        else
        {
            Bee_2.setTop(Bee_2.top()+2);
            Bee_2.setBottom(Bee_2.bottom()+2);
        }
        bee_2_move_step++;
        bee_2_move_step%=100;
        if(Bee_2.left()<=10)
        {
            int x,y;
            x=qrand()%30;
            y=qrand()%640;
            QRect rect_bee(930+x,110+y,40,40);
            Bee_2=rect_bee;
        }
    }
    update();
}

void game::IsHit_bee_1()
{
    if(vCatRect.top()>=Bee_1.top()&&vCatRect.top()<=Bee_1.bottom()&&vCatRect.right()>=Bee_1.left()&&vCatRect.right()<=Bee_1.right())
    {
        bee_hit_times++;
    }
    if(vCatRect.bottom()>=Bee_1.top()&&vCatRect.bottom()<=Bee_1.bottom()&&vCatRect.right()>=Bee_1.left()&&vCatRect.right()<=Bee_1.right())
    {
        bee_hit_times++;
    }
    if(bee_hit_times%30==0&&bee_hit_times!=0)
    {
        Hp--;
        if(Hp==0)
        {
            sDisplay="喵喵被蜜蜂蛰伤了！";
        }
    }
    update();
}

void game::IsHit_bee_2()
{
    if(vCatRect.top()>=Bee_2.top()&&vCatRect.top()<=Bee_2.bottom()&&vCatRect.right()>=Bee_2.left()&&vCatRect.right()<=Bee_2.right())
    {
        bee_hit_times++;
    }
    if(vCatRect.bottom()>=Bee_2.top()&&vCatRect.bottom()<=Bee_2.bottom()&&vCatRect.right()>=Bee_2.left()&&vCatRect.right()<=Bee_2.right())
    {
        bee_hit_times++;
    }
    if(bee_hit_times%30==0&&bee_hit_times!=0)
    {
        Hp--;
        if(Hp==0)
        {
            sDisplay="喵喵被蜜蜂蛰伤了！";
        }
    }
    update();
}

QRect game::CreateRecovery()
{
    int x,y;
    x=qrand()%800;
    y=qrand()%600;
    QRect rect(100+x,100+y,40,40);
    return rect;
}

void game::recovery_update()
{
    int x,y;
    x=qrand()%800;
    y=qrand()%600;
    QRect rect(100+x,100+y,40,40);
    recovery=rect;
    update();
}

void game::IsHit_recovery()
{
    if(vCatRect.top()>=recovery.top()&&vCatRect.top()<=recovery.bottom()&&vCatRect.right()>=recovery.left()&&vCatRect.right()<=recovery.right())
    {
        Hp=3;
    }
    if(vCatRect.bottom()>=recovery.top()&&vCatRect.bottom()<=recovery.bottom()&&vCatRect.right()>=recovery.left()&&vCatRect.right()<=recovery.right())
    {
        Hp=3;
    }
    update();
}

void game::run_time_update()
{
    run_time++;
    update();
}

void game::on_pushButton_clicked()
{
    if(!blsOver)
    {
        if(blsPause)
        {
            blsPause=false;
            sDisplay=" ";
            timer_1->start();
            update();
        }
        else
        {
            blsPause=true;
            sDisplay="喵喵要休息一回儿!";
            update();
        }
    }
}
